using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using PickGold.Charting;

namespace PickGold.Charting.Borders3D
{
	internal class SunkenBorder : IBorderType
	{
		internal float[] cornerRadius = new float[] { 15f, 15f, 15f, 15f, 15f, 15f, 15f, 15f };
		internal float defaultRadiusSize = 15f;
		internal bool drawBottomShadow = true;
		internal bool drawOutsideTopLeftShadow;
		internal bool drawScrews;
		internal float outsideShadowRate = 0.9f;
		internal float resolution = 96f;
		internal SizeF sizeLeftTop = SizeF.Empty;
		internal SizeF sizeRightBottom = SizeF.Empty;
		internal bool sunken = true;

		public virtual void AdjustAreasPosition(ChartGraphics graph, ref RectangleF areasRect)
		{
			SizeF size = new SizeF(this.sizeLeftTop);
			SizeF relativeSize = new SizeF(this.sizeRightBottom);
			size.Width += this.defaultRadiusSize * 0.7f;
			size.Height += this.defaultRadiusSize * 0.85f;
			relativeSize.Width += this.defaultRadiusSize * 0.7f;
			relativeSize.Height += this.defaultRadiusSize * 0.7f;
			size = graph.GetRelativeSize(size);
			relativeSize = graph.GetRelativeSize(relativeSize);
			if (size.Width > 30f)
			{
				size.Width = 0f;
			}
			if (size.Height > 30f)
			{
				size.Height = 0f;
			}
			if (relativeSize.Width > 30f)
			{
				relativeSize.Width = 0f;
			}
			if (relativeSize.Height > 30f)
			{
				relativeSize.Height = 0f;
			}
			areasRect.X += size.Width;
			areasRect.Width -= Math.Min(areasRect.Width, size.Width + relativeSize.Width);
			areasRect.Y += size.Height;
			areasRect.Height -= Math.Min(areasRect.Height, size.Height + relativeSize.Height);
			if (areasRect.Right > 100f)
			{
				if (areasRect.Width > (100f - areasRect.Right))
				{
					areasRect.Width -= 100f - areasRect.Right;
				}
				else
				{
					areasRect.X -= 100f - areasRect.Right;
				}
			}
			if (areasRect.Bottom > 100f)
			{
				if (areasRect.Height > (100f - areasRect.Bottom))
				{
					areasRect.Height -= 100f - areasRect.Bottom;
				}
				else
				{
					areasRect.Y -= 100f - areasRect.Bottom;
				}
			}
		}

		public virtual void DrawBorder(ChartGraphics graph, BorderSkin borderSkin, RectangleF rect, Color backColor, ChartHatchStyle backHatchStyle, string backImage, ChartImageWrapMode backImageWrapMode, Color backImageTransparentColor, ChartImageAlignmentStyle backImageAlign, GradientStyle backGradientStyle, Color backSecondaryColor, Color borderColor, int borderWidth, ChartDashStyle borderDashStyle)
		{
			RectangleF ef = graph.Round(rect);
			RectangleF ef2 = ef;
			float num = 0.3f + ((0.4f * ((borderSkin.PageColor.R + borderSkin.PageColor.G) + borderSkin.PageColor.B)) / 765f);
			Color color = Color.FromArgb((int)(backColor.R * num), (int)(backColor.G * num), (int)(backColor.B * num));
			num += 0.2f;
			Color centerColor = Color.FromArgb((int)(borderSkin.PageColor.R * num), (int)(borderSkin.PageColor.G * num), (int)(borderSkin.PageColor.B * num));
			if (borderSkin.PageColor == Color.Transparent)
			{
				centerColor = Color.FromArgb(60, 0, 0, 0);
			}
			float num2 = Math.Max(this.defaultRadiusSize, (2f * this.resolution) / 96f);
			num2 = Math.Min(num2, rect.Width / 2f);
			num2 = Math.Min(num2, rect.Height / 2f);
			num2 = (float)Math.Ceiling((double)num2);
			using (Brush brush = new SolidBrush(borderSkin.PageColor))
			{
				graph.FillRectangle(brush, rect);
			}
			if (this.drawOutsideTopLeftShadow)
			{
				ef2 = ef;
				ef2.X -= num2 * 0.3f;
				ef2.Y -= num2 * 0.3f;
				ef2.Width -= num2 * 0.3f;
				ef2.Height -= num2 * 0.3f;
				graph.DrawRoundedRectShadowAbs(ef2, this.cornerRadius, num2, Color.FromArgb(0x80, Color.Black), borderSkin.PageColor, this.outsideShadowRate);
			}
			ef2 = ef;
			ef2.X += num2 * 0.3f;
			ef2.Y += num2 * 0.3f;
			ef2.Width -= num2 * 0.3f;
			ef2.Height -= num2 * 0.3f;
			graph.DrawRoundedRectShadowAbs(ef2, this.cornerRadius, num2, centerColor, borderSkin.PageColor, this.outsideShadowRate);
			ef2 = ef;
			ef2.Width -= num2 * 0.3f;
			ef2.Height -= num2 * 0.3f;
			GraphicsPath path = graph.CreateRoundedRectPath(ef2, this.cornerRadius);
			graph.DrawPathAbs(path, backColor, backHatchStyle, backImage, backImageWrapMode, backImageTransparentColor, backImageAlign, backGradientStyle, backSecondaryColor, borderColor, borderWidth, borderDashStyle, PenAlignment.Inset);
			if (path != null)
			{
				path.Dispose();
			}
			if (this.drawScrews)
			{
				RectangleF empty = RectangleF.Empty;
				float num3 = num2 * 0.4f;
				empty.X = ef2.X + num3;
				empty.Y = ef2.Y + num3;
				empty.Width = num2 * 0.55f;
				empty.Height = empty.Width;
				this.DrawScrew(graph, empty);
				empty.X = (ef2.Right - num3) - empty.Width;
				this.DrawScrew(graph, empty);
				empty.X = (ef2.Right - num3) - empty.Width;
				empty.Y = (ef2.Bottom - num3) - empty.Height;
				this.DrawScrew(graph, empty);
				empty.X = ef2.X + num3;
				empty.Y = (ef2.Bottom - num3) - empty.Height;
				this.DrawScrew(graph, empty);
			}
			Region region = null;
			if (this.drawBottomShadow)
			{
				ef2 = ef;
				ef2.Width -= num2 * 0.3f;
				ef2.Height -= num2 * 0.3f;
				region = new Region(graph.CreateRoundedRectPath(new RectangleF(ef2.X - num2, ef2.Y - num2, ef2.Width + (0.5f * num2), ef2.Height + (0.5f * num2)), this.cornerRadius));
				region.Complement(graph.CreateRoundedRectPath(ef2, this.cornerRadius));
				graph.Clip = region;
				ef2.X -= 0.5f * num2;
				ef2.Width += 0.5f * num2;
				ef2.Y -= 0.5f * num2;
				ef2.Height += 0.5f * num2;
				graph.DrawRoundedRectShadowAbs(ef2, this.cornerRadius, num2, Color.Transparent, Color.FromArgb(0xaf, this.sunken ? Color.White : color), 1f);
				graph.Clip = new Region();
			}
			ef2 = ef;
			ef2.Width -= num2 * 0.3f;
			ef2.Height -= num2 * 0.3f;
			region = new Region(graph.CreateRoundedRectPath(new RectangleF(ef2.X + (num2 * 0.5f), ef2.Y + (num2 * 0.5f), ef2.Width - (0.2f * num2), ef2.Height - (0.2f * num2)), this.cornerRadius));
			RectangleF ef4 = ef2;
			ef4.Width += num2;
			ef4.Height += num2;
			region.Complement(graph.CreateRoundedRectPath(ef4, this.cornerRadius));
			region.Intersect(graph.CreateRoundedRectPath(ef2, this.cornerRadius));
			graph.Clip = region;
			graph.DrawRoundedRectShadowAbs(ef4, this.cornerRadius, num2, Color.Transparent, Color.FromArgb(0xaf, this.sunken ? color : Color.White), 1f);
			graph.Clip = new Region();
		}

		private void DrawScrew(ChartGraphics graph, RectangleF rect)
		{
			Pen pen = new Pen(Color.FromArgb(0x80, 0xff, 0xff, 0xff), 1f);
			graph.DrawEllipse(pen, rect.X, rect.Y, rect.Width, rect.Height);
			graph.DrawLine(pen, rect.X + ((2f * this.resolution) / 96f), (rect.Y + rect.Height) - ((2f * this.resolution) / 96f), rect.Right - ((2f * this.resolution) / 96f), rect.Y + ((2f * this.resolution) / 96f));
			pen = new Pen(Color.FromArgb(0x80, Color.Black), 1f);
			graph.DrawEllipse(pen, rect.X + ((1f * this.resolution) / 96f), rect.Y + ((1f * this.resolution) / 96f), rect.Width, rect.Height);
			graph.DrawLine(pen, rect.X + ((3f * this.resolution) / 96f), (rect.Y + rect.Height) - ((1f * this.resolution) / 96f), rect.Right - ((1f * this.resolution) / 96f), rect.Y + ((3f * this.resolution) / 96f));
		}

		public virtual RectangleF GetTitlePositionInBorder()
		{
			return RectangleF.Empty;
		}

		public virtual string Name
		{
			get
			{
				return "Sunken";
			}
		}

		public virtual float Resolution
		{
			set
			{
				this.resolution = value;
				this.defaultRadiusSize = (15f * this.resolution) / 96f;
				this.cornerRadius = new float[] { this.defaultRadiusSize, this.defaultRadiusSize, this.defaultRadiusSize, this.defaultRadiusSize, this.defaultRadiusSize, this.defaultRadiusSize, this.defaultRadiusSize, this.defaultRadiusSize };
			}
		}
	}
}

